package com.steam.job;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;
import com.steam.entity.DezhouItems;
import com.steam.entity.DezhouItemsExample;
import com.steam.entity.Items;
import com.steam.mapper.DezhouItemsMapper;
import com.steam.mapper.ItemsMapper;
import com.steam.service.JedisClient;
import com.steam.util.GsonBuilderUtil;
//@Component
public class DezhouSpringJob {
	@Autowired
	private RabbitTemplate rabbitTemplate;
	@Autowired
	private JedisClient jedisClient;
	@Autowired
	private ItemsMapper itemsMapper;
	
	private static SqlSessionFactory sqlSessionFactory;
	Gson gson = GsonBuilderUtil.create();
	static {
		String resource = "mybatis/DezhouSqlMap.xml";
		InputStream inputStream;
		try {
			inputStream = Resources.getResourceAsStream(resource);
			sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	//@Scheduled(cron = "0/2 * 7-20  * * ? ")
	public void jining() {
		SqlSession sqlSession = null;
		// 数据库会话实例
		DezhouItemsExample example = new DezhouItemsExample();
		try {
			// 创建数据库会话实例sqlSession
			sqlSession = sqlSessionFactory.openSession();
			String jishu = jedisClient.get("dezhou");
			if (StringUtils.isNotBlank(jishu)) {
				example.createCriteria().andIdGreaterThan(Long.parseLong(jishu));
			} else {

			}
			PageHelper.startPage(1, 10, false);
			PageHelper.orderBy(" id ");
			DezhouItemsMapper mapper = sqlSession.getMapper(DezhouItemsMapper.class);
			List<DezhouItems> selectByExamples = mapper.selectByExample(example);
//			List<DezhouItems> selectByExamples = sqlSession.selectList("com.steam.mapper.DezhouItemsMapper.selectByExample", example);
			for (DezhouItems tblinfo : selectByExamples) {
				Items items = new Items();
				items.setCreateTime(tblinfo.getTime());// 创建信息的时间
				items.setUserPhones(tblinfo.getuPhone());// 电话信息
				items.setContent(tblinfo.getContent());// 信息的内容
				items.setCity("德州");
				items.setFormin("德州");
				if (tblinfo.getTypeId() == 2) {// 德州的数据2是车源 目前济南的数据0是车源1是货源
					items.setTypeId(0);
					itemsMapper.insertSelective(items);
					rabbitTemplate.convertAndSend("c.联网.德州", gson.toJson(items));
				} else {
					items.setTypeId(1);
					itemsMapper.insertSelective(items);
					rabbitTemplate.convertAndSend("h.联网.德州", gson.toJson(items));
				}
				jedisClient.set("dezhou", tblinfo.getId().toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sqlSession != null) {
				sqlSession.close();
			}
		}
	}
}